<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>菜单编辑</title>
    <link rel="stylesheet" type="text/css" href="${s.base}/lib/layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/index.css" />
</head>
<body>
<div class="page-content-wrap">
    <form class="layui-form">
        <div class="layui-tab" style="margin: 0 50px 70px 30px;">
            <div class="layui-tab-content">
                <div class="layui-tab-item"></div>
                <div class="layui-tab-item layui-show">
                    <input type="hidden" class="form-control id" id="id" name="id" value="${sysMenu.id}"/>
                    <input type="hidden" class="form-control parentId" id="parentId" name="parentId" value="${sysMenu.parentId}"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">上级菜单：</label>
                        <div class="layui-input-block">
                            <input type="text" name="parentName" placeholder="" readonly value="${sysMenu.parentName!'主菜单'}" autocomplete="off" class="layui-input">
                            <input type="hidden" name="parentId" value="${sysMenu.parentId}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="red">*</span>&nbsp;菜单类型：</label>
                        <div class="layui-input-block">
                            <select name="menuType" id="menuType" lay-filter="menuType" ></select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="red">*</span>&nbsp;菜单名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="menuName" id="menuName" lay-verify="required" placeholder="请输入菜单名称" value="${sysMenu.menuName}" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item showMenuUrl" style="display: none">
                        <label class="layui-form-label"><span class="red">*</span>&nbsp;请求地址：</label>
                        <div class="layui-input-block">
                            <input type="text" name="menuUrl" id="menuUrl" placeholder="请输入请求地址" value="${sysMenu.menuUrl}" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item showMenuIcon" style="display: none">
                        <label class="layui-form-label">菜单图标：</label>
                        <div class="layui-input-block">
                            <input type="text" name="menuIcon" placeholder="请输入菜单图标" value="${sysMenu.menuIcon}" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item showPermission" style="display: none">
                        <label class="layui-form-label"><span class="red">*</span>&nbsp;权限标识：</label>
                        <div class="layui-input-block">
                            <input type="text" name="permission" id="permission" placeholder="请输入权限标识" value="${sysMenu.permission}" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="red">*</span>&nbsp;排序：</label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" id="sort" lay-verify="required" placeholder="请输入排序" value="${sysMenu.sort}" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-pop-btn">
            <div class="">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitBtn">提交</button>
                <button type="button" id="cancel" class="layui-btn layui-btn-primary">取消</button>
                <button type="button" style="display: none;" id="save" class="layui-btn layui-btn-primary"></button>
            </div>
        </div>
    </form>
</div>
<script src="${s.base}/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/js/common.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['jquery', 'form', 'page', 'dialog', 'baseutil'], function () {
        var $ = layui.jquery,
            form = layui.form,
            page = layui.page,
            dialog = layui.dialog,
            baseutil = layui.baseutil;

        page.initDict("menu_type", function (dictData) {
            page.showDict($("#menuType"), page.aceeptDictContentList(dictData, "menu_type"), "", '${sysMenu.menuType}');
        });

        if (isBlank('${sysMenu.menuType}')) {
            showPage('1');
        } else {
            showPage('${sysMenu.menuType}');
        }

        function showPage(value) {
            if ('1' === value) {
                $('.showMenuUrl').hide();
                $('input[name=menuUrl]').val('').attr('lay-verify', '');
                $('.showMenuIcon').show();
                $('.showPermission').hide();
                $('input[name=permission]').val('').attr('lay-verify', '');
            } else if ('2' === value) {
                $('.showMenuUrl').show();
                $('input[name=menuUrl]').attr('lay-verify', 'required');
                $('.showMenuIcon').show();
                $('.showPermission').hide();
                $('input[name=permission]').val('').attr('lay-verify', '');
            } else if ('3' === value) {
                $('.showMenuUrl').hide();
                $('input[name=menuUrl]').val('').attr('lay-verify', '');
                $('.showMenuIcon').hide();
                $('input[name=menuIcon]').val('').attr('lay-verify', '');
                $('.showPermission').show();
                $('input[name=permission]').attr('lay-verify', 'required');
            }
        }

        //监听下拉框
        form.on('select(menuType)', function (data) {
            showPage(data.value);
        });

        //监听上级菜单
        $('input[name=parentName]').bind('click', function () {
            var menuType = $("#menuType").val();
            if (menuType === '1') {
                menuType = '1';
            } else if (menuType === '2') {
                menuType = '1';
            } else if (menuType === '3') {
                menuType = '1,2';
            }
            dialog.popUpPage('选择上级菜单', '/base/selector/single?url=/sysMenu/dTreeList?menuType=' + menuType, '450px', '600px', function (layer, index) {
                var body = layer.getChildFrame('body', index);
                body.find("#save").bind("click", function () {
                    parent.layer.close(index);
                    var parentId = body.find("#resultId").val();
                    var parentName = body.find("#resultName").val();
                    if ('${sysMenu.id}' !== parentId) {
                        $('input[name=parentId]').val(parentId);
                        if (isBlank(parentId)) {
                            $('input[name=parentName]').val('主菜单');
                        } else {
                            $('input[name=parentName]').val(parentName);
                        }
                    }
                });
                body.find("#cancel").bind("click", function () {
                    parent.layer.close(index);
                });
            });
        });

        //监听提交
        form.on('submit(submitBtn)', function (data) {
            baseutil.post("/sysMenu/addOrUpdate", data.field, function (result) {
                if (result.code === 0) {
                    $("#save").click();
                } else {
                    dialog.msg(result.msg);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>